<?php

namespace App\Http\Controllers;

use App\Models\BookModel;
use App\Models\Classadmin;
use App\Models\ClassModel;
use App\Models\CollegeaModel;
use App\Models\ManageModel;
use App\Models\StudentModel;
use App\Models\TeacherModel;
use App\Models\Useradmin;
use App\Toor\Tranks;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class ManageController extends Tranks
{
    public function get(Request $request)
    {
        $id = $request->id;
        if ($id){
            switch ($id)
            {
                case 1:
                    return rsps(ERR_SUCCESS,StudentModel::all());
                    break;
                case 2:
                    return rsps(ERR_SUCCESS,ManageModel::all());
                    break;
                case 3:
                    return rsps(ERR_SUCCESS,TeacherModel::all());
                    break;
                default:
                    return rsps('500','null','参数错误');
                    break;
            }
        }else{
            return rsps('500','null','参数错误');
        }
    }

    public function add(Request $request)
    {
        $id = $request->addid;
        if ($id){
            switch ($id)
            {
                case 1:
                    return rsps('200',StudentModel::create($request->input()),'成功响应');
                    break;
                case 2:
                    return rsps('200',TeacherModel::create($request->input()),'成功响应');
                    break;
                case 3:
                    return rsps('200',ManageModel::create($request->input()),'成功响应');
                    break;
                default:
                    return rsps('500','null','参数错误');
                    break;
            }
        }else{
            return rsps('500','null','参数错误');
        }
    }

    public function update(Request $request)
    {

        $id = $request->addid;
        if ($id){
            switch ($id)
            {
                case 1:
                    return rsps('200',StudentModel::where('id',$request['id'])->update($request->only([
                        'id',
                        'name',
                        'studentid',
                        'phone',
                        'house',
                        'breakruleid',
                        'password',
                    ])),'成功响应');
                    break;
                case 2:
                    return rsps('200',TeacherModel::where('id',$request['id'])->update($request->only([
                        'phone',
                        'name',
                        'password',
                        'id',
                    ])),'成功响应');
                    break;
                case 3:
                    return rsps('200',ManageModel::where('id',$request['id'])->update($request->only([
                        'id',
                        'name',
                        'phone',
                        'password',
                    ])),'成功响应');
                    break;
                default:
                    return rsps('400','请求对象错误','请求对象错误');
                    break;
            }
        }else{
            return rsps('400','请求对象错误','参数错误');
        }
    }

    public function del(Request $request)
    {
        $id = $request->addid;
        if ($id){
            switch ($id)
            {
                case 1:
                    StudentModel::where('id',$request['id'])->delete();
                    return rsps('200','成功响应','成功响应');
                    break;
                case 2:
                    TeacherModel::where('id',$request['id'])->delete();
                    return rsps('200','成功响应','成功响应');
                    break;
                case 3:
                    ManageModel::where('id',$request['id'])->delete();
                    return rsps('200','成功响应','成功响应');
                    break;
                default:
                    return rsps('400','请求对象错误','请求对象错误');
                    break;
            }
        }else{
            return rsps('400','请求对象错误','参数错误');
        }
    }


    public function addcollegea(Request $request)
    {
        return rsps('200',CollegeaModel::create($request->input()),'成功响应');
//        return $request;
    }

    public function getcollegea(Request $request)
    {
        return rsps('200',CollegeaModel::paginate(2),'成功响应');
//        return $request;x
    }

    public function upcollegea(Request $request)
    {
        return rsps('200',CollegeaModel::where('id',$request['id'])->update($request),'成功响应');
//        return $request;
    }

    public function delcollegea(Request $request)
    {
        return rsps('200',CollegeaModel::where('id',$request['id'])->delete(),'成功响应');
//        return $request;
    }

    public function addstudent(Request $request)
    {
        return rsps('200',StudentModel::create($request->input()),'成功响应');
    }


    public function addclass(Request $request)
    {

        return rsps('200',ClassModel::create($request->input()),'成功响应');
//        return $request;
    }

    public function getclass(Request $request)
    {
        return rsps('200',ClassModel::paginate(2),'成功响应');
//        return $request;x
    }

    public function upclass(Request $request)
    {
        return rsps('200',ClassModel::where('id',$request['id'])->update($request),'成功响应');
//        return $request;
    }

    public function delclass(Request $request)
    {
        return rsps('200',ClassModel::where('id',$request['id'])->delete(),'成功响应');
//        return $request;
    }

    public function addbook(Request $request)
    {
        return rsps('200',BookModel::create($request->input()),'成功响应');
    }

    public function getbook(Request $request)
    {
        return rsps('200',BookModel::paginate($request['pag']),'成功响应');
    }

    public function upbook(Request $request)
    {
        return rsps('200',BookModel::where('id',$request['id'])->update($request),'成功响应');
    }

    public function delbook(Request $request)
    {
        return rsps('200',BookModel::where('id',$request['id'])->delete(),'成功响应');
    }


    public function addteacer(Request $request)
    {
        return rsps('200',TeacherModel::create($request->input()),'成功响应');
    }

    public function addmanage(Request $request)
    {
        return rsps('200',ManageModel::create($request->input()),'成功响应');
    }

    public function upstudent(Request $request)
    {
//        $redis = app('redis.connection');
//        return rsps('200',Redis::setex('la',60,'123'),'成功响应');
        return rsps('200',Redis::get('la'),'成功响应');
    }

}
